package com.suao.goods.service.impl;

import com.suao.common.constant.DelFlagConst;
import com.suao.common.utils.DateUtils;
import com.suao.common.utils.SecurityUtils;
import com.suao.goods.domain.GoodsInfo;
import com.suao.goods.mapper.GoodsInfoMapper;
import com.suao.goods.service.IGoodsInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品信息管理Service业务层处理
 *
 * @author tyz
 * @date 2023-02-27
 */
@Service
public class GoodsInfoServiceImpl implements IGoodsInfoService
{
    @Autowired
    private GoodsInfoMapper goodsInfoMapper;

    /**
     * 查询商品信息管理
     *
     * @param id 商品信息管理主键
     * @return 商品信息管理
     */
    @Override
    public GoodsInfo selectGoodsInfoById(Long id)
    {
        return goodsInfoMapper.selectGoodsInfoById(id);
    }

    /**
     * 查询商品信息管理列表
     *
     * @param goodsInfo 商品信息管理
     * @return 商品信息管理
     */
    @Override
    public List<GoodsInfo> selectGoodsInfoList(GoodsInfo goodsInfo)
    {
        return goodsInfoMapper.selectGoodsInfoList(goodsInfo);
    }

    /**
     * 新增商品信息管理
     *
     * @param goodsInfo 商品信息管理
     * @return 结果
     */
    @Override
    public int insertGoodsInfo(GoodsInfo goodsInfo)
    {
        goodsInfo.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        goodsInfo.setCreateTime(DateUtils.getNowDate());
        goodsInfo.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        goodsInfo.setUpdateTime(DateUtils.getNowDate());
        return goodsInfoMapper.insertGoodsInfo(goodsInfo);
    }

    /**
     * 修改商品信息管理
     *
     * @param goodsInfo 商品信息管理
     * @return 结果
     */
    @Override
    public int updateGoodsInfo(GoodsInfo goodsInfo)
    {
        goodsInfo.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        goodsInfo.setUpdateTime(DateUtils.getNowDate());
        return goodsInfoMapper.updateGoodsInfo(goodsInfo);
    }

    /**
     * 批量删除商品信息管理
     *
     * @param ids 需要删除的商品信息管理主键
     * @return 结果
     */
    @Override
    public int deleteGoodsInfoByIds(Long[] ids)
    {
        int count = 0;
        for (int i = 0; i < ids.length; i++) {
            GoodsInfo temp = goodsInfoMapper.selectGoodsInfoById(ids[i]);
            temp.setDelFlag(DelFlagConst.DELETED);
            temp.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
            temp.setUpdateTime(DateUtils.getNowDate());
            goodsInfoMapper.updateGoodsInfo(temp);
            count++;
        }
        return  count;
        //return goodsInfoMapper.deleteGoodsInfoByIds(ids);
    }

    /**
     * 删除商品信息管理信息
     *
     * @param id 商品信息管理主键
     * @return 结果
     */
    @Override
    public int deleteGoodsInfoById(Long id)
    {
        GoodsInfo temp = goodsInfoMapper.selectGoodsInfoById(id);
        temp.setDelFlag(DelFlagConst.DELETED);
        temp.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        temp.setUpdateTime(DateUtils.getNowDate());
        int count =  goodsInfoMapper.updateGoodsInfo(temp);
        return count;
        //return goodsInfoMapper.deleteGoodsInfoById(id);
    }
}
